﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Oblig3XNA
{
    /// <summary>
    /// Klassen representerer et objekt i solar systemet slik som Sola, planetter og satelitter
    /// </summary>
    class Planet
    {
        private Planet parent;
        private Model thisPlanet;
        private Matrix world, view, projection;
        private float size;
        private float orbitRadius;
        private float akseAngle;
        private float orbitalSpeed;
        private float orbitAngle;
        private float rotSpeed;
        public const int _MAX_ROT_HASTIGHET = 3000;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="size">Størrelse av planet</param>
        /// <param name="orbitRadius">planetens baneradius</param>
        /// <param name="akseAngle"> vinkelen til rotasjonsakse</param>
        /// <param name="orbitalSpeed">banehastiget</param>
        /// <param name="orbitAngle">vbanevinkel i forhold til xz plane</param>
        /// <param name="parent">parent planet</param>
        public Planet(float _size, float _orbitRadius, float _akseAngle, float _orbitalSpeed, float _orbitAngle, float _rotSpeed, Planet _parent)
        {
            this.size = _size;
            this.orbitRadius = _orbitRadius;
            this.akseAngle = _akseAngle;
            this.orbitalSpeed = _orbitalSpeed;
            this.orbitAngle = _orbitAngle;
            if (_rotSpeed >= 3000)
            {
                _rotSpeed = 2999;
            }
            this.rotSpeed = _MAX_ROT_HASTIGHET * (1/_rotSpeed+0.00001f);
            if (_parent != null)
            {
                this.parent = _parent;
            }

            
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="game">reference til spill, objekt tilhører</param>
        public void load(Game game, BasicEffect effect)
        {
            this.thisPlanet = game.Content.Load<Model>("sketchUp");
            this.world = effect.World;
            this.view = effect.View;
            this.projection = effect.Projection;
            
        }
        float RotY, orbRotY;
        /// <summary>
        /// 
        /// </summary>
        public void update(GameTime gt)
        {
            Matrix matIdentity, matTrans, matRotateY, matScale, matOrbT, matOrbR;
            
            matIdentity = Matrix.Identity;
            
            matScale = Matrix.CreateScale(this.size);
            
            //rotasjon om y akse
            RotY += (float)gt.ElapsedGameTime.Milliseconds / this.rotSpeed;
            RotY = RotY % (float)(2 * Math.PI);
            matRotateY = Matrix.CreateRotationY(RotY);

            //orbital rotasjon
            matOrbT = Matrix.CreateTranslation(-200f, 0f, 200f);
            orbRotY += 0.8f * (float)gt.ElapsedGameTime.Milliseconds / 5000.0f;
            orbRotY = orbRotY % (float)(2 * Math.PI);
            matOrbR = Matrix.CreateRotationY((orbRotY));

            matTrans = Matrix.CreateTranslation(0.0f, 0.0f, 0.0f);
            
            //Kumulativ world‐matrise;
            world = matIdentity * matScale * matRotateY * matOrbT * matOrbR * matTrans;
        }

        /// <summary>
        /// 
        /// </summary>
        public void draw()
        {
            thisPlanet.Draw(this.world, this.view, this.projection);
        }
    }
}
